########################################################################################################
#### Replication Script for Who are the Election Skeptics? Evidence from the 2023 Midterm Elections ####
#### Authors: Derek Holliday, Justin Grimmer, Yphtach Lelkes, Sean Westwood                         ####
#### Last Updated: 9/19/2024                                                                        ####
########################################################################################################

#### Libraries and Functions ####
# install.packages("librarian")
librarian::shelf(tidyverse, rio, here, ggthemes, pollster, tidymodels,
                 survey, srvyr, lubridate, cowplot, texreg, patchwork)
# Convenience Function for printing results without warnings:
sx = \(x) suppressWarnings(source(x, print.eval = T))
# Graphics options
sx(here("Code","funcs_theme.R"))

#### Pathing ####
# Create an R Project file in the folder containing this file (00_master.R)
# here() will reference the directory folder

#### Load Data ####
dta_midterm  = import(here("Data","dta_midterm.rds"))  # Covers respondents immediately before/after midterms
dta_accuracy = import(here("Data","dta_accuracy.rds")) # Subset with particular questions
dta_overtime = import(here("Data","dta_overtime.rds")) # Long-term data collection over several months

#### Figures and Tables ####

# Figure 1: Perceptions of accuracy and fraud by PID
sx(here("Code","figure_1.R"))

# Figure 2: Change in accuracy perception over time
sx(here("Code","figure_2.R"))

# Table 1: Change in accuracy perception
sx(here("Code","table_1.R"))

# Figure 3: Demographic representativeness (and Figure S4)
sx(here("Code","figure_3.R"))

# Figure 4: Demographic and attitudinal correlates (and Figures S6-7)
sx(here("Code","figure_4.R"))

# Table 2: Procedural "evidence" of fraud
sx(here("Code","table_2.R"))

# Figure 5: Evidence supporting skepticism
sx(here("Code","figure_5.R"))

# Figure 6: Why fraud goes unpunished
sx(here("Code","figure_6.R"))

#### Supplementary Materials ####

# Figure S1: Mean perceived accuracy
sx(here("Code","figure_s1.R"))

# Figure S2: Distribution of perceived accuracy
sx(here("Code","figure_s2.R"))

# Figure S3: Change in perceived accuracy over time by state
sx(here("Code","figure_s3.R"))

# Table S1: Stability of accuracy perceptions
sx(here("Code","table_s1.R"))

# Figure S4: More demographic and attitudinal correlates
# See figure_3.R

# Figure S5: Correlates with panel data
sx(here("Code","figure_s5.R"))

# Figure S6: Random forest predictions
# See figure_4.R

# Figure S7: Demographics only model
# See figure_4.R

